﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Management.Smo.Agent;
using Microsoft.SqlServer.Management.Smo;
using System.ServiceProcess;
using System.Data.SqlClient;

namespace www.serviciipeweb.ro.CDC
{
    public static class JobServerHelper
    {
        public static void InitializeConnection(this Server s)
        {
            SQLHelperLSN.SqlConnection = s.ConnectionContext.SqlConnectionObject.ConnectionString;

        }
        public static string ServiceName(this JobServer js)
        {
            Server s = js.Parent;
            string servicename = "SQLSERVERAGENT";
            //if (s.Name.Contains(@"\"))
            if(!string.IsNullOrEmpty(s.InstanceName))
            {
                //servicename = "SQLAgent$" + s.Name.Substring(s.Name.IndexOf(@"\") + 1);
                servicename = "SQLAgent$" + s.InstanceName;
            }
            return servicename;
        }
        public static bool IsRunning(this JobServer js)
        {
            //sysprocesses
            using (SqlConnection scon = new SqlConnection())
            {
                scon.ConnectionString = SQLHelperLSN.SqlConnection;
                scon.Open();
                using (SqlCommand sc = new SqlCommand())
                {
                    sc.CommandType = System.Data.CommandType.Text;
                    sc.CommandText = @"
                        SELECT coalesce(count(*),0) as numarprocese
                        FROM sys.dm_exec_sessions
                        WHERE LTRIM(RTRIM(program_name)) = 'SQLAgent - Generic Refresher'";
                    sc.Connection = scon;

                    using (SqlDataReader sr = sc.ExecuteReader())
                    {
                        while (sr.Read())
                        {
                            int i = int.Parse(sr[0].ToString());
                            if (i != 1)
                            {

                                return false;
                            }
                        }
                    }
                }
            }

            return true;

        }
        public static bool StartAgent(this JobServer js)
        {
            if (js.IsRunning())
                return true;

            string servicename = js.ServiceName().ToLower();

            using (ServiceController agent = ServiceController.GetServices(js.Parent.NetName).Where
                ((sco) =>
                {
                    return (sco.ServiceName.ToLower() == servicename);
                }
             ).SingleOrDefault<ServiceController>())
            {

                agent.Start();
                try
                {
                    agent.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
                }
                catch (System.ServiceProcess.TimeoutException)
                {
                    //do nothing... do not wait more...
                }

                return (agent.Status == ServiceControllerStatus.Running);
            }
        }
    }
}
